<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Executes an SQL statement directly</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.db2-escape-string.html">« db2_escape_string</a></li>
      <li style="float: right;"><a href="function.db2-execute.html">db2_execute »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.ibm-db2.html">IBM DB2 函数</a></li>
    <li>Executes an SQL statement directly</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.db2-exec" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">db2_exec</h1>
  <p class="verinfo">(PECL ibm_db2 &gt;= 1.0.0)</p><p class="refpurpose"><span class="refname">db2_exec</span> &mdash; <span class="dc-title">
   Executes an SQL statement directly
  </span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.db2-exec-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>db2_exec</strong></span>(<span class="methodparam"><span class="type">resource</span> <code class="parameter">$connection</code></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$statement</code></span>, <span class="methodparam"><span class="type">array</span> <code class="parameter">$options</code><span class="initializer"> = ?</span></span>): <span class="type">resource</span></div>



  <p class="para rdfs-comment">
   Executes an SQL statement directly.
  </p>
  <p class="para">
   If you plan to interpolate PHP variables into the SQL statement, understand
   that this is one of the more common security exposures. Consider calling
   <span class="function"><a href="function.db2-prepare.html" class="function">db2_prepare()</a></span> to prepare an SQL statement with parameter
   markers for input values. Then you can call <span class="function"><a href="function.db2-execute.html" class="function">db2_execute()</a></span>
   to pass in the input values and avoid SQL injection attacks.
  </p>
  <p class="para">
   If you plan to repeatedly issue the same SQL statement with different
   parameters, consider calling <span class="function"><a href="function.db2-prepare.html" class="function">db2_prepare()</a></span> and
   <span class="function"><a href="function.db2-execute.html" class="function">db2_execute()</a></span> to enable the database server to reuse its
   access plan and increase the efficiency of your database access.
  </p>

 </div>

 <div class="refsect1 parameters" id="refsect1-function.db2-exec-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">connection</code></dt>

     <dd>

      <p class="para">
       A valid database connection resource variable as returned from
       <span class="function"><a href="function.db2-connect.html" class="function">db2_connect()</a></span> or <span class="function"><a href="function.db2-pconnect.html" class="function">db2_pconnect()</a></span>.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">statement</code></dt>

     <dd>

      <p class="para">
       An SQL statement. The statement cannot contain any parameter markers.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">options</code></dt>

     <dd>

      <p class="para">
       An associative array containing statement options. You can use this
       parameter to request a scrollable cursor on database servers that
       support this functionality.
      </p>
      <p class="para">
       For a description of valid statement options, see 
       <span class="function"><a href="function.db2-set-option.html" class="function">db2_set_option()</a></span>.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.db2-exec-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns a statement resource if the SQL statement was issued successfully,
   or <strong><code>false</code></strong> if the database failed to execute the SQL statement.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.db2-exec-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-1255">
    <p><strong>示例 #1 Creating a table with <span class="function"><strong>db2_exec()</strong></span></strong></p>
    <div class="example-contents"><p>
     The following example uses <span class="function"><strong>db2_exec()</strong></span> to issue a set
     of DDL statements in the process of creating a table.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">db2_connect</span><span style="color: #007700">(</span><span style="color: #0000BB">$database</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$user</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$password</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Create&nbsp;the&nbsp;test&nbsp;table<br /></span><span style="color: #0000BB">$create&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'CREATE&nbsp;TABLE&nbsp;animals&nbsp;(id&nbsp;INTEGER,&nbsp;breed&nbsp;VARCHAR(32),<br />&nbsp;&nbsp;&nbsp;&nbsp;name&nbsp;CHAR(16),&nbsp;weight&nbsp;DECIMAL(7,2))'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">db2_exec</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$create</span><span style="color: #007700">);<br />if&nbsp;(</span><span style="color: #0000BB">$result</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;</span><span style="color: #DD0000">"Successfully&nbsp;created&nbsp;the&nbsp;table.\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;Populate&nbsp;the&nbsp;test&nbsp;table<br /></span><span style="color: #0000BB">$animals&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'cat'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Pook'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3.2</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'dog'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Peaches'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">12.3</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'horse'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Smarty'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">350.0</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'gold&nbsp;fish'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Bubbles'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0.1</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #0000BB">4</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'budgerigar'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Gizmo'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0.2</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #0000BB">5</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'goat'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Rickety&nbsp;Ride'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">9.7</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #0000BB">6</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'llama'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Sweater'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">150</span><span style="color: #007700">)<br />);<br /><br />foreach&nbsp;(</span><span style="color: #0000BB">$animals&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$animal</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$rc&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">db2_exec</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"INSERT&nbsp;INTO&nbsp;animals&nbsp;(id,&nbsp;breed,&nbsp;name,&nbsp;weight)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VALUES&nbsp;(</span><span style="color: #007700">{</span><span style="color: #0000BB">$animal</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]}</span><span style="color: #DD0000">,&nbsp;'</span><span style="color: #007700">{</span><span style="color: #0000BB">$animal</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]}</span><span style="color: #DD0000">',&nbsp;'</span><span style="color: #007700">{</span><span style="color: #0000BB">$animal</span><span style="color: #007700">[</span><span style="color: #0000BB">2</span><span style="color: #007700">]}</span><span style="color: #DD0000">',&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$animal</span><span style="color: #007700">[</span><span style="color: #0000BB">3</span><span style="color: #007700">]}</span><span style="color: #DD0000">)"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$rc</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;</span><span style="color: #DD0000">"Insert...&nbsp;"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Successfully created the table.
Insert... Insert... Insert... Insert... Insert... Insert... Insert... 
</pre></div>
    </div>
   </div>

   <div class="example" id="example-1256">
    <p><strong>示例 #2 Executing a SELECT statement with a scrollable cursor</strong></p>
    <div class="example-contents"><p>
     The following example demonstrates how to request a scrollable cursor for
     an SQL statement issued by <span class="function"><strong>db2_exec()</strong></span>.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">db2_connect</span><span style="color: #007700">(</span><span style="color: #0000BB">$database</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$user</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$password</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$sql&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"SELECT&nbsp;name&nbsp;FROM&nbsp;animals<br />&nbsp;&nbsp;&nbsp;&nbsp;WHERE&nbsp;weight&nbsp;&lt;&nbsp;10.0<br />&nbsp;&nbsp;&nbsp;&nbsp;ORDER&nbsp;BY&nbsp;name"</span><span style="color: #007700">;<br />if&nbsp;(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;require_once(</span><span style="color: #DD0000">'prepare.inc'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$stmt&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">db2_exec</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$sql</span><span style="color: #007700">,&nbsp;array(</span><span style="color: #DD0000">'cursor'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">DB2_SCROLLABLE</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(</span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">db2_fetch_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}&nbsp;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Bubbles
Gizmo
Pook
Rickety Ride
</pre></div>
    </div>
   </div>
   <div class="example" id="example-1257">
    <p><strong>示例 #3 Returning XML data as an SQL ResultSet</strong></p>
    <div class="example-contents"><p>
     The following example demonstrates how to work with documents stored 
     in a XML column using the SAMPLE database. Using some pretty simple 
     SQL/XML, this example returns some of the nodes in a XML document in 
     an SQL ResultSet format that most users are familiar with.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">db2_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"SAMPLE"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"db2inst1"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"ibmdb2"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$query&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'SELECT&nbsp;*&nbsp;FROM&nbsp;XMLTABLE(<br />&nbsp;&nbsp;&nbsp;&nbsp;XMLNAMESPACES&nbsp;(DEFAULT&nbsp;\'http://posample.org\'),<br />&nbsp;&nbsp;&nbsp;&nbsp;\'db2-fn:xmlcolumn("CUSTOMER.INFO")/customerinfo\'<br />&nbsp;&nbsp;&nbsp;&nbsp;COLUMNS<br />&nbsp;&nbsp;&nbsp;&nbsp;"CID"&nbsp;VARCHAR&nbsp;(50)&nbsp;PATH&nbsp;\'@Cid\',<br />&nbsp;&nbsp;&nbsp;&nbsp;"NAME"&nbsp;VARCHAR&nbsp;(50)&nbsp;PATH&nbsp;\'name\',<br />&nbsp;&nbsp;&nbsp;&nbsp;"PHONE"&nbsp;VARCHAR&nbsp;(50)&nbsp;PATH&nbsp;\'phone&nbsp;[&nbsp;@type&nbsp;=&nbsp;"work"]\'<br />&nbsp;&nbsp;&nbsp;&nbsp;)&nbsp;AS&nbsp;T<br />&nbsp;&nbsp;&nbsp;&nbsp;WHERE&nbsp;NAME&nbsp;=&nbsp;\'Kathy&nbsp;Smith\'<br />&nbsp;&nbsp;&nbsp;&nbsp;'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$stmt&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">db2_exec</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$query</span><span style="color: #007700">);<br /><br />while(</span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">db2_fetch_object</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">)){<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$row</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">CID</span><span style="color: #DD0000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">NAME</span><span style="color: #DD0000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">PHONE</span><span style="color: #DD0000">\n"</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">db2_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
1000     Kathy Smith     416-555-1358
1001     Kathy Smith     905-555-7258
</pre></div>
    </div>
   </div>
   <div class="example" id="example-1258">
    <p><strong>示例 #4 Performing a &quot;JOIN&quot; with XML data</strong></p>
    <div class="example-contents"><p>
     The following example works with documents stored in 2 different 
     XML columns in the SAMPLE database. It creates 2 temporary 
     tables from the XML documents from 2 different columns and 
     returns an SQL ResultSet with information regarding shipping 
     status for the customer.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">db2_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"SAMPLE"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"db2inst1"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"ibmdb2"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$query&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'<br />&nbsp;&nbsp;&nbsp;&nbsp;SELECT&nbsp;A.CID,&nbsp;A.NAME,&nbsp;A.PHONE,&nbsp;C.PONUM,&nbsp;C.STATUS<br />&nbsp;&nbsp;&nbsp;&nbsp;FROM<br />&nbsp;&nbsp;&nbsp;&nbsp;XMLTABLE(<br />&nbsp;&nbsp;&nbsp;&nbsp;XMLNAMESPACES&nbsp;(DEFAULT&nbsp;\'http://posample.org\'),<br />&nbsp;&nbsp;&nbsp;&nbsp;\'db2-fn:xmlcolumn("CUSTOMER.INFO")/customerinfo\'<br />&nbsp;&nbsp;&nbsp;&nbsp;COLUMNS<br />&nbsp;&nbsp;&nbsp;&nbsp;"CID"&nbsp;BIGINT&nbsp;PATH&nbsp;\'@Cid\',<br />&nbsp;&nbsp;&nbsp;&nbsp;"NAME"&nbsp;VARCHAR&nbsp;(50)&nbsp;PATH&nbsp;\'name\',<br />&nbsp;&nbsp;&nbsp;&nbsp;"PHONE"&nbsp;VARCHAR&nbsp;(50)&nbsp;PATH&nbsp;\'phone&nbsp;[&nbsp;@type&nbsp;=&nbsp;"work"]\'<br />&nbsp;&nbsp;&nbsp;&nbsp;)&nbsp;as&nbsp;A,<br />&nbsp;&nbsp;&nbsp;&nbsp;PURCHASEORDER&nbsp;AS&nbsp;B,<br />&nbsp;&nbsp;&nbsp;&nbsp;XMLTABLE&nbsp;(<br />&nbsp;&nbsp;&nbsp;&nbsp;XMLNAMESPACES&nbsp;(DEFAULT&nbsp;\'http://posample.org\'),<br />&nbsp;&nbsp;&nbsp;&nbsp;\'db2-fn:xmlcolumn("PURCHASEORDER.PORDER")/PurchaseOrder\'<br />&nbsp;&nbsp;&nbsp;&nbsp;COLUMNS<br />&nbsp;&nbsp;&nbsp;&nbsp;"PONUM"&nbsp;&nbsp;BIGINT&nbsp;PATH&nbsp;\'@PoNum\',<br />&nbsp;&nbsp;&nbsp;&nbsp;"STATUS"&nbsp;VARCHAR&nbsp;(50)&nbsp;PATH&nbsp;\'@Status\'<br />&nbsp;&nbsp;&nbsp;&nbsp;)&nbsp;as&nbsp;C<br />&nbsp;&nbsp;&nbsp;&nbsp;WHERE&nbsp;A.CID&nbsp;=&nbsp;B.CUSTID&nbsp;AND<br />&nbsp;&nbsp;&nbsp;&nbsp;B.POID&nbsp;=&nbsp;C.PONUM&nbsp;AND<br />&nbsp;&nbsp;&nbsp;&nbsp;A.NAME&nbsp;=&nbsp;\'Kathy&nbsp;Smith\'<br />'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$stmt&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">db2_exec</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$query</span><span style="color: #007700">);<br /><br />while(</span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">db2_fetch_object</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">)){<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$row</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">CID</span><span style="color: #DD0000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">NAME</span><span style="color: #DD0000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">PHONE</span><span style="color: #DD0000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">PONUM</span><span style="color: #DD0000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">STATUS</span><span style="color: #DD0000">\n"</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">db2_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
1001     Kathy Smith     905-555-7258     5002     Shipped
</pre></div>
    </div>
   </div>
   <div class="example" id="example-1259">
    <p><strong>示例 #5 Returning SQL data as part of a larger XML document</strong></p>
    <div class="example-contents"><p>
     The following example works with a portion of the PRODUCT.DESCRIPTION 
     documents in the SAMPLE database. It creates a XML document containing 
     product description (XML data) and pricing info (SQL data).
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">db2_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"SAMPLE"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"db2inst1"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"ibmdb2"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$query&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'<br />SELECT<br />XMLSERIALIZE(<br />XMLQUERY(\'<br />&nbsp;&nbsp;&nbsp;&nbsp;declare&nbsp;boundary-space&nbsp;strip;<br />&nbsp;&nbsp;&nbsp;&nbsp;declare&nbsp;default&nbsp;element&nbsp;namespace&nbsp;"http://posample.org";<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;promoList&gt;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;$prod&nbsp;in&nbsp;$doc/product<br />&nbsp;&nbsp;&nbsp;&nbsp;where&nbsp;$prod/description/price&nbsp;&lt;&nbsp;10.00<br />&nbsp;&nbsp;&nbsp;&nbsp;order&nbsp;by&nbsp;$prod/description/price&nbsp;ascending<br />&nbsp;&nbsp;&nbsp;&nbsp;return(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;promoitem&gt;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$prod,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;startdate&gt;&nbsp;{$start}&nbsp;&lt;/startdate&gt;,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;enddate&gt;&nbsp;{$end}&nbsp;&lt;/enddate&gt;,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;promoprice&gt;&nbsp;{$promo}&nbsp;&lt;/promoprice&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&lt;/promoitem&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;)<br />&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&lt;/promoList&gt;<br />\'&nbsp;passing&nbsp;by&nbsp;ref&nbsp;DESCRIPTION&nbsp;AS&nbsp;"doc",<br />PROMOSTART&nbsp;as&nbsp;"start",<br />PROMOEND&nbsp;as&nbsp;"end",<br />PROMOPRICE&nbsp;as&nbsp;"promo"<br />RETURNING&nbsp;SEQUENCE)<br />AS&nbsp;CLOB&nbsp;(32000))<br />AS&nbsp;NEW_PRODUCT_INFO<br />FROM&nbsp;PRODUCT<br />WHERE&nbsp;PID&nbsp;=&nbsp;\'100-100-01\'<br />'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$stmt&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">db2_exec</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$query</span><span style="color: #007700">);<br /><br />while(</span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">db2_fetch_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">)){<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]</span><span style="color: #DD0000">\n"</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">db2_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
&lt;promoList xmlns=&quot;http://posample.org&quot;&gt;
    &lt;promoitem&gt;
    &lt;product pid=&quot;100-100-01&quot;&gt;
        &lt;description&gt;
            &lt;name&gt;Snow Shovel, Basic 22 inch&lt;/name&gt;
            &lt;details&gt;Basic Snow Shovel, 22 inches wide, straight handle with D-Grip&lt;/details&gt;
            &lt;price&gt;9.99&lt;/price&gt;
            &lt;weight&gt;1 kg&lt;/weight&gt;
        &lt;/description&gt;
    &lt;/product&gt;
    &lt;startdate&gt;2004-11-19&lt;/startdate&gt;
    &lt;enddate&gt;2004-12-19&lt;/enddate&gt;
    &lt;promoprice&gt;7.25&lt;/promoprice&gt;
    &lt;/promoitem&gt;
&lt;/promoList&gt;
</pre></div>
    </div>
   </div>
  </p>
 </div>



 <div class="refsect1 seealso" id="refsect1-function.db2-exec-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.db2-execute.html" class="function" rel="rdfs-seeAlso">db2_execute()</a> - Executes a prepared SQL statement</span></li>
    <li class="member"><span class="function"><a href="function.db2-prepare.html" class="function" rel="rdfs-seeAlso">db2_prepare()</a> - Prepares an SQL statement to be executed</span></li>
   </ul>
  </p>
 </div>



</div></div></div></body></html>